//package com.common.http;
//
//import java.io.IOException;
//import java.net.Authenticator;
//import java.net.PasswordAuthentication;
//import java.nio.charset.Charset;
//import java.util.Arrays;
//import java.util.Base64;
//import org.apache.http.Header;
//import org.apache.http.HttpEntity;
//import org.apache.http.HttpResponse;
//import org.apache.http.client.ClientProtocolException;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClientBuilder;
//import org.apache.http.util.EntityUtils;
//
//public class SapRunRestUtil {
//	static final String kuser = "USER"; // 用户名 请注意用户 密码一定要正确，否则会报 401 unauthorized
//	static final String kpass = "PWD"; // 密码
//
//	static class MyAuthenticator extends Authenticator {
//		public PasswordAuthentication getPasswordAuthentication() {
//			return (new PasswordAuthentication(kuser, kpass.toCharArray()));
//		}
//	}
//
//	public static HttpResult httpClientWithBasicAuth(String username, String password, String uri, String param) {
//		// 创建HttpClientBuilder
//		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
//		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
//		HttpPost httpPost = new HttpPost(uri);
//		httpPost.addHeader("Content-Type", "application/json; charset=UTF-8"); // UTF-8 防中文乱码
//		// 添加Http头信息
//		httpPost.addHeader("Authorization",
//				"Basic " + Base64.getUrlEncoder().encodeToString((username + ":" + password).getBytes()));
//		httpPost.setEntity(new StringEntity(param.toString(), Charset.forName("UTF-8"))); // UTF-8 防中文乱码
//		String result = "";
//		HttpResponse httpResponse = null;
//		HttpEntity entity = null;
//		try {
//			httpResponse = closeableHttpClient.execute(httpPost);
//			entity = httpResponse.getEntity();
//			if (entity != null) {
//				result = EntityUtils.toString(entity);
//			}
//		} catch (ClientProtocolException e) {
//			System.out.println(e.getMessage());
//			e.printStackTrace();
//			return HttpResult.error(e.getMessage());
//		} catch (IOException e) {
//			System.out.println(e.getMessage());
//			e.printStackTrace();
//			return HttpResult.error(e.getMessage());
//		}
//		String httpcode = String.valueOf(httpResponse.getStatusLine().getStatusCode());
//		System.out.println("Response.getStatusLine():" + httpResponse.getStatusLine());
//		System.out.println("Http status:" + httpcode);
//
//		Header[] headers = httpResponse.getAllHeaders();
//		for (Header header : headers) {
//			System.out.println("Key:" + header.getName() + " ,Value:" + header.getValue());
//		}
//
//		System.out.println(Arrays.toString(httpResponse.getAllHeaders()));
//		// 关闭连接
//		try {
//			closeableHttpClient.close();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			return HttpResult.error(e.getMessage());
//		}
//		HttpResult httpResult = new HttpResult();
//		httpResult.setData(result);
//		return httpResult;
//	}
//
//	public static void main(String[] args) throws Exception {
//		String url = "http://<host>:<port>/RESTAdapter/Endpoint"; // 实际的接口地址， Endpoint的值可以在通道中找到
//		String a = "{......}"; // 实际要发送的JSON
//
//		String json = "";
//
//		System.out.println("JSON string = " + json.toString());
//		System.out.println(httpClientWithBasicAuth(kuser, kpass, url, json));
//	}
//}
